package com.supermap.dataservice.cloud.stat;

import com.alibaba.dubbo.config.annotation.Service;
import com.supermap.dataservice.util.JSONUtils;
import com.supermap.dubbo.api.cloud.stat.StatService;
import org.apache.logging.log4j.util.Strings;
import org.apache.poi.ss.formula.functions.T;
import sun.security.util.Length;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author aqua
 * @description
 * @date 2019年11月28 15:57
 * @email 1301584033@qq.com
 */
@Service
public class StatServiceImpl implements StatService {
    @Resource
    private StatMapper statMapper;
    @Override
    public JSONUtils getMenuClickCountList(String areaCode,Integer limit) {
        if(Strings.isBlank(areaCode))
        {
            return new JSONUtils().setMsg("功能点击统计【getMenuClickCountList】参数错误,areaCode:null,请确认参数！");
        }

        if( limit == null)
        {
            limit =100;
        }

        return new JSONUtils().setData(statMapper.getMenuClickCountList(areaCode,limit)).setCountSum(statMapper.getMenuClickCountListSumNum(areaCode));
    }

    @Override
    public JSONUtils getMenuReleaseCountList(String areaCode, String btime, String etime) {
        Map<String,Object>  maps=new HashMap<>();
        Map<String,Object>  mapProduct=new HashMap<>();
        Map<String,Object>  whatInteract=new HashMap<>();
        Map<String,Object>  basicInfo=new HashMap<>();
        //决策
        List<Map<String,Object>> mapList= statMapper.getSmsProject(btime,etime);
        Integer nums=0;
        Integer lens=areaCode.length();
        for(int i=0;i<mapList.size();i++){
            Integer num=((Long)mapList.get(i).get("num")).intValue();
            String[] codes=((String)mapList.get(i).get("fit_areaid")).split(",");
            for(int j=0;j<codes.length;j++){
                if(lens<6){
                   if(codes[j].length()>=lens){
                       if(codes[j].substring(0,lens).equals(areaCode)){
                           nums+=num;
                           break;
                       }
                   }
                }else{
                   if(codes[j].equals(areaCode)){
                       nums+=num;
                       break;
            }
        }

            }

        }
        mapProduct.put("jueceProject",nums);
        mapProduct.put("moxinProject",statMapper.getMoxinProject(areaCode,btime,etime));
        mapProduct.put("expertProject",statMapper.getExpertProject(areaCode,btime,etime));
        maps.put("serivceProduct",mapProduct);
        whatInteract.put("agr",statMapper.getAgrNums(areaCode,btime,etime));
        whatInteract.put("dis",statMapper.getDisasterNums(areaCode,btime,etime));
        whatInteract.put("adv",statMapper.getAdvNums(areaCode,btime,etime));
        maps.put("whatInteract",whatInteract);
        basicInfo.put("zhuti",statMapper.getZhutiNums(areaCode,btime,etime));
        basicInfo.put("expertNums",statMapper.getExpertNums(areaCode,btime,etime));
        basicInfo.put("menhu",statMapper.getMenhuNums(areaCode,btime,etime));
        basicInfo.put("farm",statMapper.getFarmNums(areaCode,btime,etime));
        maps.put("basicInfo",basicInfo);
        return new JSONUtils().setData(maps);
    }

    @Override
    public JSONUtils getMenuGroupByCountList(String areaCode, String btime, String etime,Integer state) {
        List<Map<String,Object>> mapList=null;
        if(state==1){
            //决策  区县+产品组统计
            mapList=statMapper.getGroupJuece(areaCode,btime,etime);
        }
        if(state==2){
            //模型  区县+产品组统计
            mapList=statMapper.getGroupMoxin(areaCode,btime,etime);
        }
        if(state==3){
            //专家  区县+产品组统计
            mapList=statMapper.getGroupZhuanjia(areaCode,btime,etime);
        }
        if(state==4||state==5||state==6){
            //互动咨询
            mapList=statMapper.getGroupHudongzixun(areaCode,btime,etime);
        }
        if(state==7){
            //农业主体
            mapList=statMapper.getGroupZhuti(areaCode,btime,etime);
        }
        if(state==8){
            //专家
            mapList=statMapper.getGroupZj(areaCode,btime,etime);
        }
        if(state==9){
            //门户
            mapList=statMapper.getGroupMenhu(areaCode,btime,etime);
        }
        if(state==10){
            //农田
            mapList=statMapper.getGroupFarm(areaCode,btime,etime);
        }

        mapList.stream().forEach(map ->{
            String names[]=((String)map.get("names")).split(",");
            String nums[]=((String)map.get("nums")).split(",");
            List<Map<String,Object>> mapList1=new ArrayList<>();
            for(int i=0;i<names.length;i++){
                Map<String,Object> map1=new HashMap<>();
                map1.put("name",names[i]);
                map1.put("num",(Integer.valueOf(nums[i])));
                mapList1.add(map1);
            }
            map.put("list",mapList1);
        });
        return new JSONUtils().setData(mapList);
    }


    @Override
    public JSONUtils getFarmingList(String areaCode, Integer userId, Integer farmId, Integer page, Integer size) {
        return new JSONUtils().setData(statMapper.getFarmingList(areaCode,userId,farmId,page,size));
    }


}
